package com.x.crm.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.x.crm.dao.IntentionModelDao;
import com.x.crm.dao.OrderCarDao;
import com.x.crm.model.CustomerShop;
import com.x.crm.model.IntentionModel;
import com.x.crm.model.OrderCar;
import com.x.crm.rpc.IntentionModelRpcService;
import com.x.crm.rpc.IntentionRpcService;
import com.x.erp.model.Car;
import com.x.erp.util.ConstantState;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.service.BaseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Service(interfaceName = "com.x.crm.rpc.IntentionModelRpcService", timeout = 60000)
public class IntentionModelService extends BaseService implements IntentionModelRpcService {

	@Autowired
	private IntentionModelDao intentionModelDao;

	@Autowired
	private  OrderCarDao  orderCarDao;

	public List<IntentionModel> selectIntentionModelList(IntentionModel where) {
		try {
			return intentionModelDao.selectIntentionModelList(where);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0011, e);
		}
	}

	public int selectIntentionModelNum(IntentionModel where) {
		try {
			return intentionModelDao.selectIntentionModelNum(where);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0011, e);
		}
	}

	public List<IntentionModel> selectModelList(IntentionModel intentionModel, CustomerShop customerShop) {
		try {
			return intentionModelDao.selectModelList(intentionModel, customerShop);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0011, e);
		}
	}

	public List<IntentionModel> selectGroupBySeriesList(IntentionModel where) {
		try {
			return intentionModelDao.selectGroupBySeriesList(where);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0011, e);
		}
	}

	public IntentionModel insertIntentionModel(IntentionModel intentionModel) {
		try {
			return intentionModelDao.insert(intentionModel);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0012, e);
		}
	}

	public int updateIntentionModel(IntentionModel set, IntentionModel where) {
		try {
			return intentionModelDao.update(set, where);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0013, e);
		}
	}

	public List<IntentionModel> selectIntentionCustomerList(IntentionModel intentionModel) {
		try {
			return intentionModelDao.selectIntentionCustomerList(intentionModel);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0011, e);
		}
	}

	public void updateCarState(Car car) {
		try {
			IntentionModel intentionModel = new IntentionModel();
			intentionModel.setCarId(car.getCarId());
			OrderCar orderCar = new OrderCar();
			orderCar.setCarId(car.getCarId());
			List<IntentionModel> modelList = intentionModelDao.selectIntentionModelList(intentionModel);
			List<OrderCar> orderCarList = orderCarDao.selectOrderCarList(orderCar);
			if (modelList.size() > 0) {
				IntentionModel intentionModelInfo = new IntentionModel();
				boolean updateState = false;
				if (car.getCarSaleState() != null && car.getCarSaleState() == ConstantState.CAR_SALE_STATE_OFFSALE) {
					intentionModelInfo.setCarState(0);
					updateState = true;
				}
				if (!(StringUtils.isEmpty(car.getCarTitle()) && car.getCarRetailPrice() == null && StringUtils.isEmpty(car.getCarImageUrl()))) {
					intentionModelInfo.setCarTitle(car.getCarTitle());
					intentionModelInfo.setCarRetailPrice(car.getCarRetailPrice());
					intentionModelInfo.setCarImageUrl(car.getCarImageUrl());
					updateState = true;
				}
				if (updateState) {
					IntentionModel whereIntentionModel = new IntentionModel();
					whereIntentionModel.setCarId(car.getCarId());
					this.update(intentionModelInfo, whereIntentionModel);
				}
			}
			if (orderCarList.size() > 0 ){
				OrderCar orderCarInfo = new OrderCar();
				boolean updateState = false;
				if (car.getCarSaleState() != null && car.getCarSaleState() == ConstantState.CAR_SALE_STATE_OFFSALE) {
					orderCarInfo.setCarState(0);
					updateState = true;
				}
				if (!(StringUtils.isEmpty(car.getCarTitle()) && car.getCarRetailPrice() == null && StringUtils.isEmpty(car.getCarImageUrl()))) {
					orderCarInfo.setCarTitle(car.getCarTitle());
					orderCarInfo.setCarRetailPrice(car.getCarRetailPrice());
					orderCarInfo.setCarImageUrl(car.getCarImageUrl());
					updateState = true;
				}
				if (updateState) {
					OrderCar whereOrderCar = new OrderCar();
					whereOrderCar.setCarId(car.getCarId());
					this.update(orderCarInfo, whereOrderCar);
				}
			}
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0013, e);
		}
	}


	/**
	 * ERP删除车辆CRM 已有的汽车 全部状态变为0
	 * @param car
	 */
	public void deleteCarOpr(Car car) {
		try {
			IntentionModel intentionModel = new IntentionModel();
			intentionModel.setCarId(car.getCarId());
			List<IntentionModel> modelList = intentionModelDao.selectIntentionModelList(intentionModel);
			if (modelList.size() > 0) {
				intentionModel = new IntentionModel();
				intentionModel.setCarState(0);
				IntentionModel whereIntentionModel = new IntentionModel();
				whereIntentionModel.setCarId(car.getCarId());
				this.update(intentionModel, whereIntentionModel);
			}
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0013, e);
		}
	}


}
